package day01;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/2/28 9:46
 * 二分查找
 */
public class SingleRecursion04 {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 5, 6, 9, 12};
        int index = search(arr, 5);
        System.out.println(index);
    }

    public static int search(int[] arr, int target) {
        return f(arr, target, 0, arr.length - 1);
    }

    public static int f(int[] arr, int target, int i, int j) {
        if (i > j) {
            return -1;
        }
        int m = (i + j) >>> 1;
        if (target < arr[m]) {
            return f(arr, target, i, m-1);
        } else if (arr[m] < target) {
            return f(arr, target, m+1, j);
        } else {
            return m;
        }
    }
}
